package thread;

/**
 * @author ten_fei
 * @create 2021-02-15 20:54
 * <p>
 * <p>
 * 例子：创建三个窗口卖票，总票数为100
 */
public class WindowTest {
    public static void main(String[] args) {
//        Window1 w1 = new Window1();
//        Window1 w2 = new Window1();
//        Window1 w3 = new Window1();
//
//        w1.setName("窗口一");
//        w2.setName("窗口二");
//        w3.setName("窗口三");
//
//        w1.start();
//        w2.start();
//        w3.start();
        Window2 win2 = new Window2();
        Thread t1 = new Thread(win2);
        Thread t2 = new Thread(win2);
        Thread t3 = new Thread(win2);

        t1.start();
        t2.start();
        t3.start();
    }
}

class Window1 extends Thread {
    private static int ticket = 100;

    public void run() {
        while (true) {
            if (ticket > 0) {
                System.out.println(getName() + "卖票，票号为：" + ticket);
                ticket--;
            } else {
                break;
            }
        }
    }
}

class Window2 implements Runnable {
    private static int ticket = 100;

    public void run() {
        while (true) {
            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + "卖票，票号为：" + ticket);
                ticket--;
            } else {
                break;
            }
        }
    }
}















